# -*- coding:utf-8 -*-
# coding=utf-8
# encoding: utf-8
from model.avinfo import avinfo
from db.db import database
d=database()
import pymysql
import sys
import os
import importlib
import requests
importlib.reload(sys)
import  uuid
import imghdr
from lxml import etree
from bs4 import BeautifulSoup
CurrentPath = os.getcwd()
dirs=os.path.split( os.path.realpath( sys.argv[0] ))

startpage=1
headers = {'content-type': 'application/json',
           'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36',
           'Accept':'image/webp,image/apng,image/*,*/*;q=0.8',
           'Cookie':'surferid=f2b7a5e13f02f981a145721f204e5e36; imps19218=1; juicy_data_1=YToxOntpOjU2NDkzNTtpOjE0OTcxODEyOTA7fQ%3D%3D; juicy_data=YToxOntzOjEyOiJqdWljeV9kYXRhXzEiO3M6MToiMSI7fQ%3D%3D; visid_incap_165243=Itwi/IicRGa6iE8UxXzzz+k3OVkAAAAAQUIPAAAAAACBZT7oHkLiFrqTcygTG9NE; nlbi_165243=xKnTMf7AXxTFy3U168vTAgAAAADtRcRuVLE5BQw7Fw7b3NaC; incap_ses_574_165243=JhPSXlojiSp80fpWNEL3B+o3OVkAAAAAxLb3WwnGASq5Qv0rF1BHCw==',
           'Host':'ads-a.juicyads.com'
           }
#cookies = dict(playno1='playno1Cookie', playno1_referer='/')
for j in range(1,200):
    page=requests.get("https://avio.pw/cn/released/page/"+str(j),headers=headers)
    html=page.content
    nodes= BeautifulSoup(html,"lxml")
    items=nodes.find_all(attrs={"class":"movie-box"})

    for item in items:
        av=avinfo()
        print("图片LOGO:"+(item.find("img"))["src"])
        av.LogoURL=(item.find("img"))["src"];
        print("页面地址:" + item['href'])
        av.MovieDetailUrl=item['href']
        detailpage=requests.get(av.MovieDetailUrl,headers=headers)
        #print(detailpage.content)
        detailnodes = BeautifulSoup(detailpage.content, "lxml", from_encoding='utf-8')
        avatarbox=detailnodes.find("a","avatar-box")
        if avatarbox!=None :
            av.StarName=avatarbox.find("span").text;
            print("明星:"+av.StarName)
        detail = detailnodes.find("div","col-md-3 info")
        detailcontents=detail.find_all("p")
        print(len(detailcontents))
        av.Code=detailcontents[0].find_all('span')[1].string
        print("神秘代码:"+av.Code)
        av.PublishDatetime = str(detailcontents[1].text).replace("发行时间: ","")
        print(av.PublishDatetime)
        av.Maker = str(detailcontents[4].text).replace("制作商: ","")
        if len(av.Maker)==0 :
            av.Maker=str(detailcontents[5].text).replace("制作商: ","")
        print("制作商:"+av.Maker)
        av.Publisher=str(detailcontents[6].text).replace("发行商: ","")
        if len(av.Publisher) == 0:
            av.Publisher = str(detailcontents[7].text).replace("发行商: ", "")
        print("发行商:"+av.Publisher)
        av.BigImage=detailnodes.find("a","bigImage").get('href')
        print("标题图片:"+av.BigImage)
        if av.StarName!=None and len(str(av.StarName))!=0:
            result = d.getavstar(av.StarName)
            if result != None:
                av.AvStarID=result["Id"]
            else:
                d.insertavstar(av.StarName)
                result = d.getavstar(av.StarName)
                if result != None:
                    av.AvStarID = result["Id"]
        else:
            av.AvStarID=-1
            av.StarName="未知"
        result=d.getavmovie(av.Code)
        if result == None:
            print("插入数据库："+str(d.insertavmovie(av)))
        else:
            print(av.Code+"已经在数据库了")

